package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.User;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;
    @Override
    @Transactional
    public User login(String phone, String code, String codeInSession) {
        User dbUser=null;
        //1. 校验验证码是否为空并且两个验证码是否一致
        if ( codeInSession != null && code.equalsIgnoreCase(codeInSession)){
            //验证没问题
            //2. 根据手机号查询用户
            System.out.println(codeInSession);
           dbUser =userMapper.findByPhone(phone);
            //3. 如果为空代表该用户是新用户,直接注册
            if (dbUser==null){
                dbUser = new User();
                dbUser.setPhone(phone);
                dbUser.setStatus(1);
                userMapper.save(dbUser);
            }

        }

        return dbUser;
    }
}
